package service

import (
	"blog_server/global"
	"mime/multipart"
	"time"
)

func (svc *Service) UploadFileToOSS(fh *multipart.FileHeader) error {
	file, err := fh.Open()
	if err != nil {
		return err
	}
	key := time.Now().Format("20060102150405-") + fh.Filename
	err = global.OSSBucket.PutObject(key, file)
	return err
}

func (svc *Service) GetImgList() ([]string, error) {
	listObjects, err := global.OSSBucket.ListObjects()
	if err != nil {
		return nil, err
	}
	var res []string
	for _, v := range listObjects.Objects {
		res = append(res, v.Key)
	}
	return res, nil
}

func (svc *Service) DelImg(fileName string) error {
	return global.OSSBucket.DeleteObject(fileName)
}
